/**
 * 
 * 本类作用：为获取对象的属性提供统一的代码
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.attributes;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import ext.generic.erp.bean.AlternativeMaterial;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;
import wt.change2.WTChangeOrder2;
import wt.iba.value.DefaultAttributeContainer;
import wt.iba.value.IBAHolder;
import wt.iba.value.litevalue.AbstractValueView;
import wt.iba.value.service.IBAValueHelper;
import wt.part.WTPart;
import wt.part.WTPartUsageLink;
import wt.util.WTException;

public class AttributeUtil {
	
	private static String clazz = AttributeUtil.class.getName() ;
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	/**
	 * 获取对象的属性值
	 * 
	 * @param obj
	 * @param attributeName
	 * @return String
	 */
	public static String getAttribute( Object obj , String attributeName ){
		String attributeValue = "" ;
		
		StringBuffer logBuffer = new StringBuffer("\n") ;
		try{
			if(obj == null){
				PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj is NULL!", IntegrationConstant.LOG_LEVEL_WARN ) ;
				
				return attributeValue ;
			}else if( attributeName == null || attributeName.trim().equals("") ){
				PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), attributeName is NULL!", IntegrationConstant.LOG_LEVEL_WARN ) ;
				
				return attributeValue ;
			}else{
				if(verbose){
					logBuffer.append("indetifier:");
				}
				
				if( obj instanceof WTPart ){
					//获取WTPart的属性值
					PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj instanceof WTPart!", IntegrationConstant.LOG_LEVEL_NORMAL ) ;
					
					WTPart part = ( WTPart ) obj ;
					
					if(verbose){
						logBuffer.append(part.toString());
						logBuffer.append("\n");
					}
					
					attributeValue = PartAttribute.getAttribute(part, attributeName) ;
				}else if( obj instanceof WTPartUsageLink ){
					PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj instanceof WTPartUsageLink!", IntegrationConstant.LOG_LEVEL_NORMAL ) ;
					
					WTPartUsageLink usageLink = ( WTPartUsageLink ) obj ;
					
					if(verbose){
						logBuffer.append(usageLink.toString());
						logBuffer.append("\n");
					}
					
					attributeValue = PartUsageLinkAttribute.getAttribute(usageLink, attributeName) ;
				}else if( obj instanceof WTChangeOrder2 ){
					PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj instanceof WTChangeOrder2!", IntegrationConstant.LOG_LEVEL_NORMAL ) ;
					
					WTChangeOrder2 ecn = ( WTChangeOrder2 ) obj ;
					
					if(verbose){
						logBuffer.append(ecn.toString());
						logBuffer.append("\n");
					}
					
					attributeValue = ECNAttribute.getAttribute(ecn, attributeName) ;
				}else if( obj instanceof AlternativeMaterial ){
					PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj instanceof ReplaceMaterial!", IntegrationConstant.LOG_LEVEL_NORMAL ) ;
					
					AlternativeMaterial replaceMaterial = ( AlternativeMaterial ) obj ;
					
					if(verbose){
						logBuffer.append(replaceMaterial.toString());
						logBuffer.append("\n");
					}
					
					attributeValue = AlternativeMaterialUtil.getAttribute(replaceMaterial, attributeName) ;
				}else {
					PDMIntegrationLogUtil.printLog(clazz, "getAttribute( Object obj , String attributeName ), obj instanceof " + obj.getClass() , IntegrationConstant.LOG_LEVEL_WARN ) ;
					attributeValue = "" ;
				}
			}
		}finally{
			if(verbose){
				String logInfo = logBuffer.toString() ;
				
				logInfo = "\n getAttribute( Object obj , String attributeName ) \n" ;
				logInfo = "attribute Name : "  + attributeName + " \n" ;
				logInfo = "attribute Value : " + attributeValue + " \n" ;
				PDMIntegrationLogUtil.printLog(clazz, logInfo , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
			}
		}
		
		return attributeValue ;
	}
	
	/**
	 * 获取IBA属性值表
	 * @param ibaHolder IBA储存器，例如文档、零部件、图档等
	 * @return
	 * @throws WTException
	 * @throws java.rmi.RemoteException
	 */
	public static List<String> getIBANamesList(IBAHolder ibaHolder) {
		List<String> ibaNamesList = new ArrayList<String>() ;
		
		try{
			ibaHolder = IBAValueHelper.service.refreshAttributeContainerWithoutConstraints(ibaHolder);
			DefaultAttributeContainer dac = (DefaultAttributeContainer) ibaHolder.getAttributeContainer();
			AbstractValueView valueViews[] = dac.getAttributeValues();
			
			for (int j = 0; j < valueViews.length; j++) {
				AbstractValueView avv = valueViews[j];
				
				String ibaLogicalID =  avv.getDefinition().getLogicalIdentifier();
				
				System.out.println(">>>>>> Get ibaLogicalID : " + ibaLogicalID ) ;
				
				ibaNamesList.add(ibaLogicalID) ;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}finally{
			
		}
		
		return ibaNamesList ;
	}
	
	
	/**
	 * 取IBAHolder的所有IBA属性值:
	 *
	 * String result[i][0]: 第i个属性的名称<br>
	 * String result[i][1]: 第i个属性的值
	 *
	 * @param ibaHolder
	 * @return
	 */
	public static List<String> getIBANamesLite(IBAHolder ibaHolder){
		List<String> ibaNameList = new ArrayList<String>() ;
		
		if( ibaHolder == null ){
			return ibaNameList ;
		}
		
		DefaultAttributeContainer dac = (DefaultAttributeContainer) ibaHolder.getAttributeContainer();
		if (dac == null) {
			try {
				ibaHolder = IBAValueHelper.service.refreshAttributeContainer(ibaHolder, null, null, null);
				dac = (DefaultAttributeContainer) ibaHolder.getAttributeContainer();
			} catch (Exception e) {
				e.printStackTrace();
				return ibaNameList ;
			}
		}

		if( dac == null ){
			return ibaNameList ;
		}
			
		AbstractValueView[] avv = dac.getAttributeValues();
		
		if ( avv == null){
			return ibaNameList ;
		}

		for (int i = 0; i < avv.length; i++) {
			String ibaName = avv[i].getDefinition().getLogicalIdentifier() ;
			
			ibaNameList.add( ibaName ) ;
		}

		return ibaNameList;
	}
}
